﻿using AutoMapper;
using CodeSaude.Application.Interfaces;
using CodeSaude.Application.ViewModels;
using CodeSaude.Domain.Entities;
using CodeSaude.Domain.Interfaces.Service;
using System;
using System.Collections.Generic;

namespace CodeSaude.Application.Services
{
    public class ExameApplicationService : BaseApplicationService, IExameApplicationService
    {
        private readonly IExameService _exameService;

        public ExameApplicationService(IExameService exameService)
        {
            _exameService = exameService;
        }

        public void Create(ExameViewModel model)
        {
            try
            {
                Exame exame = Mapper.Map<ExameViewModel, Exame>(model);

                BeginTransaction();
                _exameService.Add(exame);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Dispose()
        {
            _exameService.Dispose();
        }

        public IEnumerable<ExameViewModel> GetAll()
        {
            try
            {
                return Mapper.Map<IEnumerable<ExameViewModel>>(_exameService.GetAll());
            }
            catch (Exception)
            {
                throw;
            }
        }

        public ExameViewModel GetById(int id)
        {
            try
            {
                return Mapper.Map<ExameViewModel>(_exameService.GetById(id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        public void Delete(ExameViewModel model)
        {
            try
            {
                Exame exame = _exameService.GetById(model.ExameId);
                BeginTransaction();
                _exameService.Remove(exame);
                Commit();

            }
            catch (Exception)
            {
                throw;
            }
        }

        public void Update(ExameViewModel model)
        {
            try
            {
                Exame exame = Mapper.Map<Exame>(model);
                BeginTransaction();
                _exameService.Update(exame);
                Commit();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
